/**
 * Created by Eric.Dou on 4/14/14.
 */

define(function (require) {
    var comm = require('lib/commons');
    return  (function (helper, $) {
        var _SG = this, handler = {
            registeName: [],
            actions: [],
            conditions: []
        } , defaultOrderWithAction = function (options) {
            console.log("Starting");
        };
        this.createDefaultAction = function () {
            return {
                processName: "default",
                process: defaultOrderWithAction,
                condition: false
            };
        };
        this.createDefaultProcessChain = function () {
            return {
                value: this.createDefaultAction(),
                prev: null,
                next: null
            };
        };

        var _processChain = this.createDefaultProcessChain(), top = _processChain;

        this.getChain = function () {
            return top;
        };
        this.getOrderActions = function (options) {
            var index = 0, actArray = [];
            $.each(handler.actions, function (i, pro) {
                if ($.isFunction(pro.condition) && pro.condition(options)) {
                    actArray[index++] = pro.process;
                } else if (pro.condition) {
                    actArray[index++] = pro.process;
                }
            });
            return actArray;
        };

        this.createAction = function (name, action) {
            var _name, _action;
            switch (arguments.length) {
                case 1:
                    if ($.isFunction(name)) {
                        _name = comm.getFuncName(name);
                        _action = name;
                    }
                    else if (typeof(name) === 'Object') {
                        _name = name.toString();
                        _action = function () {
                            return name
                        }
                    }
                    else if (typeof(name) === 'String') {
                        _name = name;
                        _action = function () {
                            return name
                        };
                    }
                    break;
                case 2:
                    _name = name;
                    _action = action;
                    break;
                default :
                    _name = name;
                    _action = action;
                    break;
            }
            console.info(_action);

            return {
                processName: _name,
                process: _action,
                condition: _action.condition != undefined ? _action.condition : true
            }
        };

        this.registAction = function (name, action) {
            return _registProcess(this.createAction(name, action));
        };
        this.registProcessWithDefaultChain = function (chain, action) {
            var clianElement = {value: action, prev: chain};
            if (typeof(chain) !== 'undefined' && chain != null) {
                chain.next = clianElement;
            }
            return clianElement;
        };
        function _pushArray(obj, name, value) {
            if (obj[name] != undefined) {
                if ($.isArray(obj[name])) {
                    obj[name].push(value);
                } else if (typeof(obj[name]) == 'object' || typeof(obj[name]) == 'string') {
                    newArray = [obj[name], value];
                    obj[name] = newArray;
                }
            } else {
                obj[name] = [value];
            }
        }

        this.registHandler = function (h, process) {
            _pushArray(h, 'registeName', process.processName);
            _pushArray(h, 'actions', process);
            _pushArray(h, 'conditions', process.condition);
            return _SG
        }

        function _registProcess(process) {
            _processChain = this.registProcessWithDefaultChain(_processChain, process);
            this.registHandler(handler, process);
            return _SG
        }

        this.hasName = function (name) {
            console.log(handler.registeName);
            var flag = false;
            $.each(handler.registeName, function (j, data) {
                if (flag == false) {
                    if (name == data) {
                        //TODO need deep traverse the action array
                        flag = true;
                        return flag;
                    }
                }
            });
            return flag;
        };

        this.find = function (name) {
            var targetAction = null, acArray = handler.actions;

            $.each(handler.registeName, function (i, data) {
                if (name == data) {
                    //TODO need deep traverse the action array
                    if (acArray.length >= i) {
                        action = acArray[i];
                        console.log("find the action from SG inner~~ " + data);
                        console.log(action);
                        if (action.processName == name) {
                            targetAction = action;
                            return true;
                        }
                    }
                }
            });
            return targetAction;
        };
        helper.SG = _SG;
    });
});
